package com.caiyouxi.util;

import com.alibaba.druid.util.StringUtils;
import com.caiyouxi.boss.ClientInfo;
import com.caiyouxi.boss.HostInfo;
import com.caiyouxi.boss.LogBean;
import com.caiyouxi.common.util.Log;
import eu.bitwalker.useragentutils.UserAgent;
import org.nutz.json.Json;
import org.nutz.lang.Lang;
import org.nutz.mvc.Mvcs;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;

public class LogUtil {
    public static void i(String target_id, String target_type, String action,String user_id,
                         String result_data,String raw_data,String level,String product_code,String app_id){
        ClientInfo clientInfo = new ClientInfo();
        clientInfo.setIp(Lang.getIP(Mvcs.getReq()));
        clientInfo.setAgent(getAgent(Mvcs.getReq()));
        clientInfo.setMac(getMACAddress(Lang.getIP(Mvcs.getReq())));
        clientInfo.setClient_id("-");
        LogBean log = new LogBean();
        log.setCategory("USER");
        log.setLevel(level);
        if(!StringUtils.isEmpty(action))
        {
            action = action.toUpperCase();
        }
        log.setAction(action);
        log.setTarget_type(target_type);
        log.setTarget_id(target_id);
        log.setUser_id(user_id);
        log.setResult_data(Json.toJson(result_data));
        log.setRaw_data(Json.toJson(raw_data));
        log.setChlient_info(Json.toJson(clientInfo));
        log.setProduct_code(product_code);
        String ip = null;
        String mac = null;
        String host = null;
        try{
            InetAddress ia = InetAddress.getLocalHost();
            ip = ia.toString();
            mac = getLocalMac(ia);
            host = InetAddress.getLocalHost().getHostName();
        }catch (Exception e){
            e.printStackTrace();
            Log.e("获取IP异常");
        }
        HostInfo hInfo = new HostInfo();
        hInfo.setIp(ip);
        hInfo.setHost(host);
        hInfo.setMac(mac);
        hInfo.setApp_id(app_id);
        log.setHost_info(Json.toJson(hInfo));
        LogThread logThread = new LogThread(log);
        logThread.start();

    }

    public static String getMACAddress(String ip) {
        String str = "";
        String macAddress = "";
        return macAddress;
    }




    public static String getAgent(HttpServletRequest request) {

        if (!Lang.isEmpty(request)) {
            UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
            return "操作系统版本（" + userAgent.getOperatingSystem() + "）；浏览器版本（" + userAgent.getBrowser() + "-" + userAgent.getBrowserVersion() + "）";
        } else {
            return "";
        }
    }

    private static String getLocalMac(InetAddress ia) throws SocketException {
        // TODO Auto-generated method stub
        //获取网卡，获取地址
        byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
        System.out.println("mac数组长度："+mac.length);
        StringBuffer sb = new StringBuffer("");
        for(int i=0; i<mac.length; i++) {
            if(i!=0) {
                sb.append("-");
            }
            //字节转换为整数
            int temp = mac[i]&0xff;
            String str = Integer.toHexString(temp);
//            System.out.println("每8位:"+str);
            if(str.length()==1) {
                sb.append("0"+str);
            }else {
                sb.append(str);
            }
        }
        System.out.println("本机MAC地址:"+sb.toString().toUpperCase());
        return sb.toString().toUpperCase();
    }

}
